package com.chongba.job;


import com.chongba.entity.Constants;
import com.chongba.entity.ResponseMessage;
import com.chongba.feign.TaskServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
@Slf4j
public class JobService {

    @Autowired
    private TaskServiceClient taskServiceClient;
    @Autowired
    private SelectMaster selectMaster;

    @Scheduled(cron = "*/1 * * * * ?") //注意要将chongba_schedule_service中的@Scheduled注释
    public void refresh(){
        // 只有主节点才去统一调度刷新
        if(selectMaster.checkMaster(Constants.job_leaderPath)){
            log.info("job主节点开始进行刷新任务调度");
            try {
                ResponseMessage refresh = taskServiceClient.refresh();
                log.info("refresh---,{}",refresh);
            } catch (Exception e) {
                log.error("refresh exception {}",e.getMessage());
            }
        }else {
            log.info("job从节点备用");
        }
    }

    @PostConstruct
    private void init(){
        //争抢注册主节点
        selectMaster.selectMaster(Constants.job_leaderPath);
    }
}
